/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.gui.util;



public class TimeUtil {
	
    public static String[] MONTH_ABBRS_UC = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT",
        "NOV", "DEC" };

public static int getMonthValue(String abbr) {
    abbr = abbr.toUpperCase();
    int month = -1;
    for (int i = 0; i < MONTH_ABBRS_UC.length; i++) {
        if (abbr.equals(MONTH_ABBRS_UC[i])) {
            month = i + 1;
            break;
        }
    }
    return month;
}


public static boolean checkTimestampFormat(String ts) {
	try {
		String[] tokens = ts.split(" ");

		if (tokens.length == 2) {
			// Should be a timestamp
			if (checkDateFormat(tokens[0])) {
				return checkTimeFormat(tokens[1]);

			}

		} else {
			// Just a date
			return checkDateFormat(tokens[0]);
		}
	} catch (Exception e) {
		//e.printStackTrace();
	}
	return false;
}

public static boolean checkDateFormat(String date) {
	try {
		String[] tokens = date.split("-");
		int y = 0;
		int m = 0;
		int d = 0;
		if (tokens.length == 3) {
			// See if it is YYYY-MM-DD
			 y = Integer.parseInt(tokens[0]);
			 m = Integer.parseInt(tokens[1]);
			 d = Integer.parseInt(tokens[2]);
             if (tokens[0].length() != 4)
                 return false;
		} else {
			// See if it is MM/DD/YYYY
			tokens = date.split("/");
			 m = Integer.parseInt(tokens[0]);
			 d = Integer.parseInt(tokens[1]);
			 y = Integer.parseInt(tokens[2]);
             if (tokens[2].length() != 4)
                 return false;
		}
		//if (y < 1998)
		//	return false;
		if (m > 12|| m < 1)
			return false;
		if (d > 31 || d < 1)
			return false;
		return true;
	} catch (Exception e) {
		//e.printStackTrace();
	}
	return false;
}

public static boolean checkTimeFormat(String time) {
	try {
		String[] tokens = time.split(":");
		for (int i = 0; i < tokens.length; i++) {
			switch (i) {
			case 0:
				int h = Integer.parseInt(tokens[i]);
				if (h > 23 || h < 0)
					return false;
				break;
			case 1:
				int m = Integer.parseInt(tokens[i]);
				if (m > 59 || m < 0)
					return false;
				break;
			case 2:
				float s = Float.parseFloat(tokens[i]);
				if (s < 60.0 && s >= 0.0) {

				} else
					return false;
				break;
			}
		}
		return true;

	} catch (Exception e) {
		//e.printStackTrace();
	}
	return false;
}

public static String normalizeTimestampFormat(String ts, boolean isStart) {
	
	try {
		String date = null;
		String time = null;
		ts = ts.trim();
		String[] tokens = ts.split(" ");
		date = normalizeDateFormat(tokens[0]);
		if (date == null) {
			return null;
		}
		if (tokens.length == 2) {
			// Should be a timestamp
			time = normalizeTimeFormat(tokens[1]);
		} else if (isStart) {
				time = "00:00:00.0";
		} else {
		    time = "23:59:59.999";
		}
		
		return date + " " + time;
	} catch (Exception e) {
		//e.printStackTrace();
	}
	return null;
}

public static String normalizeDateFormat(String date) {
	try {
		String[] tokens = date.split("-");
		int y = 0;
		int m = 0;
		int d = 0;
		if (tokens.length == 3) {
			// See if it is YYYY-MM-DD
			 y = Integer.parseInt(tokens[0]);
			 m = Integer.parseInt(tokens[1]);
			 d = Integer.parseInt(tokens[2]);
		} else {
			// See if it is MM/DD/YYYY
			tokens = date.split("/");
			 m = Integer.parseInt(tokens[0]);
			 d = Integer.parseInt(tokens[1]);
			 y = Integer.parseInt(tokens[2]);
		}
		//if (y < 2001)
		//	return null;
		if (m > 12|| m < 1)
			return null;
		if (d > 31 || d < 1)
			return null;
		return y + "-" + m + "-" + d;
	} catch (Exception e) {
		//e.printStackTrace();
	}
	return null;
}

public static String normalizeTimeFormat(String time) {
	try {
		String[] tokens = time.split(":");
		int h = 0;
		int m = 0;
		float s = (float)0.0;
		for (int i = 0; i < tokens.length; i++) {
			switch (i) {
			case 0:
				 h = Integer.parseInt(tokens[i]);
				if (h > 23 || h < 0)
					return null;
				break;
			case 1:
				 m = Integer.parseInt(tokens[i]);
				if (m > 59 || m < 0)
					return null;
				break;
			case 2:
				 s = Float.parseFloat(tokens[i]);
				if (s < 60.0 && s >= 0.0) {

				} else
					return null;
				break;
			}
		}
		return h + ":" + m + ":" + s;

	} catch (Exception e) {
		//e.printStackTrace();
	}
	return null;
}

}
